﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeTester
{
    /// <summary>
    /// 双链表结点
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class DoubleLinkedListNode<T>
    {
        //结点数据
        public T Item { get; set; }
        //结点指针 单链表指点的是上一结点
        public DoubleLinkedListNode<T> Prev { get; set; }
        //结点指针 单链表指点的是下一结点
        public DoubleLinkedListNode<T> Next { get; set; }

        public DoubleLinkedListNode() { }
        public DoubleLinkedListNode(T item)
        {
            this.Item = item;
        }
    }

    public class DoubleLinkedList<T>
    {
        private int count; // 字段：当前链表节点个数
        private DoubleLinkedListNode<T> head; // 字段：当前链表的头结点

        public int Count
        {
            get
            {
                return this.count;
            }
        }

        /// <summary>
        /// 索引器
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public T this[int index]
        {
            get
            {
                return this.GetNodeByIndex(index).Item;
            }
            set
            {
                this.GetNodeByIndex(index).Item = value;
            }
        }

        /// <summary>
        /// 根据索引取得节点
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public DoubleLinkedListNode<T> GetNodeByIndex(int index)
        {
            if (index < 0 || index >= this.count)
            {
                throw new ArgumentOutOfRangeException("index","out of the range");
            }
            DoubleLinkedListNode<T> tempNode = this.head;
            for (int i = 0; i < index; i++)
            {
                tempNode = tempNode.Next;
            }

            return tempNode;
        }

        public DoubleLinkedList()
        {
            this.count = 0;
            this.head = null;
        }

        /// <summary>
        /// 在最后结点之后添加节点
        /// </summary>
        /// <param name="value"></param>
        public void AddAfter(T value)
        {
            DoubleLinkedListNode<T> tempNode = new DoubleLinkedListNode<T>(value);
            if (this.count == 0)
            {
                this.head = tempNode;
            }
            else
            {
                DoubleLinkedListNode<T> lastNode = this.GetNodeByIndex(this.count - 1);
                lastNode.Next = tempNode;
                tempNode.Prev = lastNode;
            }
            this.count++;
        }
        /// <summary>
        /// 在最后结点之前添加节点
        /// </summary>
        /// <param name="value"></param>
        public void AddBefore(T value)
        {
            DoubleLinkedListNode<T> tempNode = new DoubleLinkedListNode<T>(value);
            if (this.count == 0)
            {
                this.head = tempNode;
            }
            else
            {
                DoubleLinkedListNode<T> lastNode = this.GetNodeByIndex(this.count - 1);
                DoubleLinkedListNode<T> preNode = lastNode.Prev;

                preNode.Next = tempNode;
                tempNode.Prev = preNode;

                tempNode.Next = lastNode;
                lastNode.Prev = tempNode;
   
            }
            this.count++;
        }

        /// <summary>
        /// 在指定位置之后插入节点
        /// </summary>
        /// <param name="index"></param>
        /// <param name="value"></param>
        public void InsertAfter(int index, T value)
        {
             DoubleLinkedListNode<T> tempNode = new DoubleLinkedListNode<T>(value);
             if (index == 0)
             {
                 if (this.count == 0)
                 {
                     this.head = tempNode;
                 }
                 else
                 {
                     this.head.Next = tempNode;
                     tempNode.Prev = this.head; 
                 }
             }
             else
             {
                 DoubleLinkedListNode<T> curNode = this.GetNodeByIndex(index);
                 DoubleLinkedListNode<T> curNextNode = curNode.Next;

                 curNode.Next = tempNode;
                 tempNode.Prev = curNode;

                 if (curNextNode != null)
                 {
                     tempNode.Next = curNextNode;
                     curNextNode.Prev = tempNode;
                 }
             }
             this.count++;
        }
        /// <summary>
        /// 在指定位置之前插入节点
        /// </summary>
        /// <param name="index"></param>
        /// <param name="value"></param>
        public void InsertBefore(int index,T value)
        {
            DoubleLinkedListNode<T> tempNode=new DoubleLinkedListNode<T>(value);
            if (index == 0)
            {
                if (this.count == 0)
                {
                    this.head = tempNode;
                }
                else
                {
                    tempNode.Next = this.head;
                    this.head.Prev = tempNode;

                    this.head = tempNode;
                }
            }else
            {
                DoubleLinkedListNode<T> curNode = this.GetNodeByIndex(index);
                DoubleLinkedListNode<T> curPreNode = curNode.Prev;

                curPreNode.Next = tempNode;
                tempNode.Prev = curPreNode;

                tempNode.Next = curNode;
                curNode.Prev = tempNode;
            }
            this.count++;
        }

        public void RemoveAt(int index)
        {
            if (index == 0)
            {
                this.head = this.head.Next;
            }
            else
            {
                DoubleLinkedListNode<T> delNode = this.GetNodeByIndex(index);
                DoubleLinkedListNode<T> delPreNode = delNode.Prev;
                DoubleLinkedListNode<T> delNextNode = delNode.Next;

                delPreNode.Next = delNextNode;
                if (delNextNode != null)
                {
                    delNextNode.Prev = delPreNode;
                }
                delNode = null;
            }
            this.count--;
        }
    }
}
